import BaseAudioManager from "../cfw/audio/BaseAudioManager";


export default class MusicManager extends BaseAudioManager {
    tempName: string;
    play(name: string, isLoop: boolean = true) {
        if (this.tempName == name) {
            return;
        }

        this.stop();
        this.tempName = name;
        super.play(name, isLoop);
    }

    getMaxNum(){
        return  cc.audioEngine.getMaxAudioInstance();
    }

    add(name: string, res, isLoop: boolean) {
        this.tempName = name;
        let audioId = cc.audioEngine.playMusic(res, isLoop);
        this.sourcePool.set(name, audioId);
        if (!isLoop) {
            cc.audioEngine.setFinishCallback(audioId, this.stop.bind(this, name));
        }
    }

    pause() {

        cc.audioEngine.pauseMusic();
    }

    resume() {

        cc.audioEngine.resumeMusic();
    }

    stop() {

        if (!this.tempName) {
            return;
        }

        cc.audioEngine.stopMusic();
        let audioId = this.sourcePool.get(this.tempName);
        if (audioId) {
            this.sourcePool.delete(this.tempName);
        } else {
            console.log(' MusicManager audioId is null ')
        }
        this.tempName = null;

    }

    setVolume(count: number) {

        cc.audioEngine.setMusicVolume(count);

    }

    isPlaying() {
        return cc.audioEngine.isMusicPlaying();
    }

    clear() {
        this.stop()
    }

}
